package klimuts.periodicals.user.actions;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import klimuts.periodicals.data.DataSource;
import klimuts.periodicals.user.User;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Represents form with two fields for login user.
 * If data base return {@link User} by login and password then
 * it {@link User} been saved to session.<br>
 * If {@link User} is administrator then user will be redirected
 * to administrator page
 * @author Siarhei Klimuts
 */
public class Login extends ActionSupport implements SessionAware {
	private Map<String,Object> session;
	private String login;
	private String password;	


	public String execute() throws Exception {
		User user=DataSource.getUser(login, password);
		if (user!=null) {
			session.put("user", user);
			if (user.getRights()>0)
				return "admin";
			else
				return "success";
		} else
			return "login";
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
